﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GB2312Inquriy
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //test();
            comboBox_Selector.SelectedIndex=0;
            textBox_Inquiry.Text = "㙟";
            textBox_code.Text = "82 30 AD 31";
        }

        private void btnInquiry_Click(object sender, EventArgs e)
        {
            if (textBox_Inquiry.TextLength > 0)
            {
                textBox_GBK.Text = getGBK(textBox_Inquiry.Text);
                textBox_Unicode.Text = getUnicode(textBox_Inquiry.Text);
                textBox_UTF8.Text = getUTF8(textBox_Inquiry.Text);
                //String strInquiry = textBox_Inquiry.Text;
                //byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(strInquiry);
                //String strResult = String.Empty;
                //foreach (byte b in bytes)
                //{
                //    strResult += b.ToString("X2");
                //    strResult += " ";
                //}
                //textBox_GBK.Text = strResult;
            }
            else
            {
                MessageBox.Show("请输入待查询的字符");
            }
            if (textBox_code.TextLength>0)
            {
                button_Inquiry_Click(sender, e);
            }
        }
        /// <summary>
        /// 按ESC键退出程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape)
            {
                Application.Exit();
            }
        }
        private string getGBK(string strIn)
        {
            byte[] by = Encoding.GetEncoding("GB18030").GetBytes(strIn);
            string strResult = string.Empty;
            foreach (byte b in by)
            {
                strResult += b.ToString("X2");
                strResult += " ";
            }
            return strResult;
        }
        private string getUnicode(string strIn)
        {
            byte[] by = Encoding.BigEndianUnicode.GetBytes(strIn);
            string strResult = string.Empty;
            foreach (byte b in by)
            {
                strResult += b.ToString("X2") + " ";
            }
            return strResult;
        }
        private string getUTF8(string strIn)
        {
            byte[] by = Encoding.UTF8.GetBytes(strIn);
            string strResult = string.Empty;
            foreach (byte b in by)
            {
                strResult += b.ToString("X2") + " ";
            }
            return strResult;
        }
        private void test()
        {
            {//显示汉字的GBK编码
	            string s = "我";
	            byte[] by=Encoding.GetEncoding("GBK").GetBytes(s);
	            String strResult = String.Empty;
	            foreach (byte b in by)
	            {
	                strResult += b.ToString("X2");
	                strResult += " ";
	            }
            }
            {//显示汉字的Unicode编码
                string s = "汉";
                byte[] by = Encoding.Unicode.GetBytes(s);
                String strResult = String.Empty;
                foreach (byte b in by)
                {
                    strResult += b.ToString("X2");
                    strResult += " ";
                }
            }
            {//显示汉字的UTF8编码
                string s = "汉";
                byte[] by = Encoding.GetEncoding("UTF-8").GetBytes(s);
                String strResult = String.Empty;
                foreach (byte b in by)
                {
                    strResult += b.ToString("X2");
                    strResult += " ";
                }
            }
        }

        private void comboBox_Selector_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strText = textBox_code.Text;
  
            
            if (strText.Length>0)
            {
                switch (comboBox_Selector.SelectedIndex)
                {
                    case 0:
                        {
                            strText = FromatText(strText,2);
                            if (string.IsNullOrEmpty(strText))
                            {
                                MessageBox.Show("格式不对");
                                return;
                            }
                            textBox_Han.Text = getHanFromGBK(strText);
                        }
                        break;
                    case 1:
                        {
                            strText = FromatText(strText,2);
                            if (string.IsNullOrEmpty(strText))
                            {
                                MessageBox.Show("格式不对");
                                return;
                            }
                            textBox_Han.Text = getHanFromUnicode(strText);
                        }
                        break;
                    case 2:
                        {
                            strText = FromatText(strText,3);
                            if (string.IsNullOrEmpty(strText))
                            {
                                MessageBox.Show("格式不对");
                                return;
                            }
                            textBox_Han.Text = getHanFromUTF8(strText);
                        }
                        break;
                }
            }
        }
        /// <summary>
        /// 两个一组 以空格分开 前提 必须为2的倍数
        /// </summary>
        /// <param name="strText"></param>
        /// <returns></returns>
        private string FromatText(string strText,int iLen)
        {
            strText = RemoveNotNumber(strText);
            if (strText.Length.Equals(0))
            {
                return string.Empty;
            }
            if (strText.Length%(iLen*2)!=0)
            {
                return string.Empty;
            }
            strText = strText.Replace(" ", "").ToUpper();
            string strResult=string.Empty;
            strResult+=strText[0];
            for (int i = 1; i < strText.Length;i++ )
            {
                if (i%2  == 0)
                {
                    strResult += " ";
                }
                strResult += strText[i];
            }
            return strResult;
            //throw new NotImplementedException();
        }
        /// <summary>
        /// 去掉字符串中的非数字
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string RemoveNotNumber(string key)
        {
            //return System.Text.RegularExpressions.Regex.Replace(key, @"[^\d]*", "");
            //string sss = System.Text.RegularExpressions.Regex.Replace(key, @"[^0-9a-fA-F]*", "");
            return System.Text.RegularExpressions.Regex.Replace(key, @"[^0-9a-fA-F]*", "");
        }
        private string getHanFromGBK(string strGBK)
        {
            string strResult = strGBK.Replace(" ", "");
            string[] ss = strGBK.Split(' ');
            byte[] hexByte = new byte[strResult.Length/2];
            for (int i = 0; i < hexByte.Length; i++)
            {
                hexByte[i] = (byte)Convert.ToInt32(strResult.Substring(i * 2, 2), 16);
            }
            return Encoding.GetEncoding("GB18030").GetString(hexByte);
            //byte[] bs = new byte[2];
            //for (int i = 0; i < ss.Length; i++)
            //{
            //    bs[0] = Convert.ToByte(ss[i], 16);
            //    bs[1] = Convert.ToByte(ss[i+1], 16);
            //    strResult += Encoding.GetEncoding("GB18030").GetString(bs);
            //    i++;
            //}
            
            //return strResult;
        }
        private string getHanFromUnicode(string strUnicode)
        {
            string strResult = string.Empty;
            string[] ss = strUnicode.Split(' ');
            byte[] bs = new byte[2];
            for (int i = 0; i < ss.Length;i++ )
            {
                bs[0] = Convert.ToByte(ss[i], 16);
                bs[1] = Convert.ToByte(ss[i+1], 16);
                strResult += Encoding.BigEndianUnicode.GetString(bs);
                i++;
            }
            
            return strResult;
        }
        private string getHanFromUTF8(string strUTF8)
        {
            string strResult = string.Empty;
            string[] ss = strUTF8.Split(' ');
            byte[] bs = new byte[3];
            for (int i = 0; i < ss.Length;i++ )
            {
                bs[0] = Convert.ToByte(ss[i++], 16);
                bs[1] = Convert.ToByte(ss[i++], 16);
                bs[2] = Convert.ToByte(ss[i], 16);
                strResult += Encoding.UTF8.GetString(bs);
            }

            return strResult;
        }

        private void button_Inquiry_Click(object sender, EventArgs e)
        {
            comboBox_Selector_SelectedIndexChanged(sender, e);
        }
    }
}
